package com.cyy;

/**
 * @author 会玩的洋洋
 * @since 2022-02-22
 * @param <E>
 */
public class DoubleCircleLinkedList<E> extends AbstractList<E> {
    /**
     * 头结点
     */
    private Node<E> firstNode;
    /**
     * 指针节点
     */
    private Node<E> current;
    /**
     * 尾结点
     */
    private Node<E> lastNode;

    /**
     * 重置指针
     */
    public void reset() {
        current = firstNode;
    }

    /**
     * 指针指向下一个
     * @return E
     */
    public E next() {
        if (current == null) {
            return null;
        }
        current = current.next;
        return current.element;
    }

    @Override
    public void clear() {
        firstNode = null;
        lastNode = null;
        size = 0;
    }

    @Override
    public E get(int index) {
        return node(index).element;
    }

    @Override
    public E set(int index, E element) {
        Node<E> node = node(index);
        E oldElement = node.element;
        node.element = element;
        return oldElement;
    }

    @Override
    public void add(int index, E element) {
        rangeCheckAdd(index);
        if (index == size) {
            // 向最后插入
            Node<E> oldLast = lastNode;
            lastNode = new Node<>(oldLast, firstNode, element);
            if (oldLast == null) {
                // 第一次添加
                firstNode = lastNode;
                firstNode.next = firstNode;
                firstNode.prev = firstNode;
            } else {
                oldLast.next = lastNode;
                firstNode.prev = lastNode;
            }
        } else {
            // 正常情况
            Node<E> node = node(index);
            Node<E> prev = node.prev;
            Node<E> newNode = new Node<>(prev, node, element);
            node.prev = newNode;
            prev.next = newNode;
            if (index == 0) {
                // 如果在第一个位置添加
                firstNode = newNode;
            }
        }
        ++size;
    }

    /**
     * 删除当前指针所指向的节点
     */
    public E remove() {
        if (current == null) {
            return null;
        }
        Node<E> next = current.next;
        E element = remove(current);
        if (size == 0) {
            current = null;
        } else {
            current = next;
        }
        return element;
    }

    @Override
    public E remove(int index) {
        rangeCheck(index);
        return remove(node(index));
    }

    private E remove(Node<E> node) {
        if (size == 1) {
            // 删除 最后一个节点
            firstNode = lastNode = null;
        } else {
            node.prev.next = node.next;
            node.next.prev = node.prev;
            if (node == firstNode) {
                // 删除的头节点
                firstNode = node.next;
            }
            if (node == lastNode) {
                // 删除的尾节点
                lastNode = node.prev;
            }
        }
        --size;
        return node.element;
    }

    @Override
    public int indexOf(E element) {
        Node<E> node = firstNode;
        if (element == null) {
            for (int i = 0; i < size; i++) {
                if (node.element == null) {
                    return i;
                }
                node = node.next;
            }
        } else {
            for (int i = 0; i < size; i++) {
                if (node.element.equals(element)) {
                    return i;
                }
                node = node.next;
            }
        }
        return FAILED_RETURN;
    }

    private Node<E> node(int index) {
        rangeCheck(index);
        Node<E> node = null;
        if (index < (size >> 1)) {
            node = firstNode;
            for (int i = 0; i < index; i++) {
                node = node.next;
            }
        } else {
            node = lastNode;

            for (int i = size - 1; i > index; i--) {
                node = node.prev;
            }
        }
        return node;
    }

    private static class Node<E> {
        Node<E> prev;
        Node<E> next;
        E element;

        public Node() {
        }

        public Node(Node<E> prev, Node<E> next, E element) {
            this.prev = prev;
            this.next = next;
            this.element = element;
        }

        @Override
        public String toString() {
            return "Node{" +
                    "element=" + element +
                    '}';
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("DoubleCircleLinkedList{")
                .append("firstNode=")
                .append(firstNode)
                .append(", lastNode=").append(lastNode);
        Node<E> node = firstNode;
        for (int i = 0; i < size; i++) {
            sb.append(", element=")
                    .append(node.element);
            node = node.next;
        }
        sb.append(", size=")
                .append(size)
                .append("}");
        return sb.toString();
    }
}
